package com.openxt.nbzj.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.openxt.nbzj.R;
import com.openxt.nbzj.activity.WebViewActivity;
import com.openxt.nbzj.adapter.NewsWorkListAdapter;
import com.openxt.nbzj.model.ConfigModel;
import com.openxt.nbzj.model.RemotingModel;
import com.openxt.nbzj.remoting.RemotingEx;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.BindView;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import qs.openxt.libs.util.ActivityUtil;
import qs.openxt.libs.util.StringUtil;
import qs.openxt.libs.util.Utils;

/**
 * Created by 颖杰_2 on 2016/5/31.
 */
public class ContentNewsFragment extends OpenxtFragment implements View.OnClickListener, RemotingEx.OnActionListener, AdapterView.OnItemClickListener {
    @BindView(R.id.news_widget)
    ListView news_widget;
    @BindView(R.id.line_up_img)
    ImageView line_up_img;

    RemotingModel remotingModel = null;
    Handler mHandler = new Handler();
    NewsWorkListAdapter newsViewAdapter = null;
    List<JSONObject> newsData = new ArrayList<>();
    Context mContext;

    HomeFragment homeFragment;

    public void setHomeFragment(HomeFragment homeFragment) {
        this.homeFragment = homeFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.content_item_news, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mContext = getActivity();
        remotingModel = new RemotingModel(mContext);
        initView();
    }

    private void initView() {
        newsViewAdapter = new NewsWorkListAdapter(mContext, newsData);
        newsViewAdapter.setTransparent(true);
        news_widget.setAdapter(newsViewAdapter);
        news_widget.setOnItemClickListener(this);
        line_up_img.setOnClickListener(this);

        initNewsGethot();

        if (ActivityUtil.isNetworkAvailable(mContext)) {
            remotingModel.getNewsGethot(this, false);
        }
    }

    private void initNewsGethot(){
        String val = Utils.getLocalStorage(mContext, ConfigModel.NEWS_GETHOT);
        if (StringUtil.isNull(val))
            return;
        try {
            final JSONArray data = JSON.parseArray(val);
            newsData.clear();
            newsViewAdapter.notifyDataSetChanged();
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0, j = data.size(); i < j; i++) {
                        JSONObject item = data.getJSONObject(i);
                        newsData.add(item);
                    }
                    newsViewAdapter.notifyDataSetChanged();
                }
            }, 0);
        } catch (Exception e) {

        }
    }
    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.line_up_img){
            homeFragment.setCurrentItem(1);
        }
    }

    @Override
    public void refresh() {

    }

    @Override
    public void willBeDisplayed() {

    }

    @Override
    public void onActionCompleted(String reVal, String actionName) {
        Utils.setLocalStorage(mContext, actionName, reVal);
        if (ConfigModel.NEWS_GETHOT.equals(actionName)) {
            initNewsGethot();
        }
    }

    @Override
    public void onActionFailed(String errorVal, String actionName) {
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(!ActivityUtil.isNetworkAvailable(mContext)){
            showMsg("无网络，请先查看网络是否正常打开");
            return;
        }
        Utils.setLocalStorage(mContext, ConfigModel.NATIVE_PAGENAME, getActionUrl());
        Bundle bundle = new Bundle();
        JSONObject item = newsData.get(position);
        String RedirectURL = item.getString("RedirectURL");
        bundle.putString("url", ConfigModel.ROOTPATH + RedirectURL);

        toActivity(WebViewActivity.class, bundle, false);
    }
}
